package listtest;

import java.util.Arrays;

public class MyArraylist implements Ilist{
    public int[] array;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyArraylist(){
        this.array = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.usedSize] = data;
        this.usedSize++;
    }
    private void grow(){
        this.array = Arrays.copyOf(this.array,2*this.array.length);
    }

    @Override
    public boolean isFull() {
        return this.usedSize==array.length;
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
            if(isFull()){
                grow();
            }
            for (int i = this.usedSize-1; i >= pos ; i--) {
                array[i+1] =array[i];
            }
            array[pos] = data;
            this.usedSize++;
        }catch (PosIllegal e){
            System.out.println("插入元素pos位置不合法");
            e.printStackTrace();
        }
    }
    private void checkPos(int pos) throws PosIllegal{
        if(pos < 0 || pos > this.usedSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(array[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws PosIllegal{
        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }
    public void checkEmpty(){
        if (isEmpty()){
            throw new EmptyException("顺序表为空");
        }
    }
    public boolean isEmpty(){
        return this.usedSize==0;
    }
    private void checkPos2(int pos) throws PosIllegal{
        if(pos < 0 || pos >= this.usedSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }


    @Override
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos] =value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try{
            checkEmpty();
            int pos =indexOf(toRemove);
            if(pos == -1){
                return;
            }
            for (int i = pos; i < this.usedSize-1 ; i++) {
                array[i] = array[i+1];
            }
            usedSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
//        for (int i = 0; i < usedSize; i++) {
//            array[i] = null;
//        }
        usedSize = 0;

    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i] + " ");

        }

    }
}
